home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / cfuncs.zip / SELECT.C < prev    next >
Text File  |  1991-07-09  |  4KB  |  136 lines

  1. /*----------------------------select-------------------------------*/
  2. /*DESCRIPTION: Displays text and allows the user to choose one     */
  3. /*    of the displayed lines.                       */
  4. /*                                   */
  5. /*INPUT:
  6.     X & Y - coordinates the upper left corner of the first
  7.         choice appears at
  8.     TotChoices - number of choices
  9.     Choices - string of chars, 1 char for each choice & the char
  10.           for the chosen choice is returned.
  11.     Text - array of pointers to char that point to the text
  12.         to be displayed
  13.     TextColor, BGcolor - foreground & background text color
  14.     BarColor - color of selection bar
  15.     BarTextColor - color of text in bar
  16.     Abort - 0 accept Esc key
  17.     Abort - 1 don't accept Esc key
  18.     Abort - 10 accept Esc & print double space
  19.     Abort - 11 don't accept Esc key & print double space
  20.     default - char from list of choices to specify the default bar
  21. RETURNS: corresponding char in Choices of selection.
  22.     or 27 if Esc pressed
  23. USES: strspc, GetCursor, OffCursor, SetCursor
  24. -------------------------------------------------------------------*/
  25.  
  26. char select(int X, int Y, int TotChoices, char *Choices, char *Text[],
  27.         int TextColor, int BGcolor, int BarColor,
  28.         int BarTextColor, int Abort, char defalt)
  29. {
  30.  
  31.     int    MaxChoi, CurBar, MovBar, i, len, BarLen = 0, Choice, a;
  32.     unsigned int    OldCursor, spacing;
  33.     char *strspc(char *s, int n);
  34.  
  35. /*---------PRINT THE CHOICES-----------------*/
  36.     if (Abort >=10)
  37.       spacing = 2;
  38.     else
  39.       spacing = 1;
  40.  
  41.     for(i = 0; i < TotChoices; ++i)
  42.     {
  43.       len = strlen(Text[i]);
  44.       if(len > BarLen) BarLen = len;
  45.     }
  46.     BarLen++;
  47.     for(i = 0; i < TotChoices; ++i)
  48.       WriteAt(X, Y + i*spacing, TextColor, BGcolor, " %-*s",
  49.             BarLen, Text[i]);
  50.  
  51.     OldCursor = GetCursor();
  52.     OffCursor();
  53.  
  54.  
  55.     MaxChoi = TotChoices -1;
  56.     CurBar = 0;
  57.     /* initialize the bar to the default parameter */
  58.     for(i=0; i<TotChoices; ++i)
  59.       if(defalt== Choices[i]) {
  60.         MovBar = i;
  61.         break; }
  62.     if(i>=TotChoices)
  63.       MovBar = 0;
  64.  
  65.     Choice = -1;
  66.  
  67.     for(;1==1;)
  68.     {
  69.       /*--------CLEAR OLD BAR--------*/
  70.       if(CurBar != MovBar)
  71.       {
  72.         WriteAt(X, Y + CurBar*spacing, TextColor, BGcolor, " %-*s",
  73.             BarLen, Text[CurBar]);
  74.       }
  75.  
  76.       /*---------DISPLAY NEW BAR--------*/
  77.       WriteAt(X, Y + MovBar*spacing, 4, BarColor, "%c", 16);
  78.       WriteAt(X+1, Y + MovBar*spacing, BarTextColor, BarColor, "%-*s",
  79.         BarLen, Text[MovBar]);
  80.  
  81. /*      textbackground(BarColor);
  82.       gotoxy(X, Y + MovBar*spacing);
  83.       textcolor(4);
  84.       putch(16);
  85.       textcolor(BarTextColor);
  86.       cputs(line[MovBar]); */
  87.       CurBar = MovBar;
  88.  
  89.       if(Choice > 0 ) break;
  90.  
  91.     /*-------- GET INPUT---------*/
  92.       a = toupper(GetIdleCh());
  93.       if(a == 0)  a = 256 + getch();
  94.       for(i = 0; i< TotChoices; ++i)
  95.         if(a == Choices[i])
  96.         {
  97.           Choice = Choices[i];
  98.           MovBar = i;
  99.         }
  100.  
  101.       if( a == 27 && (Abort) % 10 != 1)        /* Esc key */
  102.       {
  103.          SetCursor(OldCursor);
  104.          return(a); /* return 27 for Esc */
  105.       }
  106.       else if(a == 13)    /* enter pressed */
  107.         Choice = Choices[CurBar];
  108.       else if(a == 327)     /* home key */
  109.         MovBar = 0;
  110.       else if(a == 335)    /* end key */
  111.         MovBar = MaxChoi;
  112.       else if(a == 336 || a == 333)    /* down or right arrow */
  113.         MovBar = CurBar + 1;
  114.       else if(a == 328 || a == 331) /* up or left arrow */
  115.         MovBar = CurBar -1;
  116.  
  117.       if(MovBar > MaxChoi) MovBar = 0;
  118.       if(MovBar < 0) MovBar = MaxChoi;
  119.  
  120.     }
  121.  
  122.     SetCursor(OldCursor);    /* return cursor to its original shape */
  123.  
  124.     return(Choice);
  125. }
  126.  
  127. /* main()
  128. {
  129.   char *text[] = {"option uno", "option dose a mondo rama jama",
  130.           "option thrace"};
  131.   char choices[] = "123";
  132.   int abort;
  133.  
  134.   NewClear(1, 0);
  135.   select(30, 8, 3, choices, text, 15, 1, 3, 7, &abort);
  136. } */